package com.zx.cloud.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.RequestLogging;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.domain.ClientBankCard;
import com.zx.cloud.domain.ClientContact;
import com.zx.cloud.domain.ClientInfo;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.service.ClientContactService;
import com.zx.cloud.service.ClientInfoService;
import com.zx.cloud.service.LogService;
import com.zx.cloud.util.Page;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Description:
 *
 * @author       Yan Weiwei
 * @Create Time: 2017年10月30日上午11:30:15
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/clientInfo")
public class ClientInfoController {
	
	@Autowired
	private ClientInfoService clientInfoService;
	
	@Autowired
	private ClientContactService clientContactService;
	
	@Autowired
	private LogService logService;
	
	@RequestLogging
	@ApiOperation(value = "查询会员列表信息" , notes = "查询会员列表信息")
	@RequestMapping(value = "/queryClientInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response queryClientInfo(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody ClientInfo clientInfo){
		
		clientInfo.setOrderBy("ZX_CLIENT_INFO.create_time DESC");//降序
		Page<ClientInfo> datas=clientInfoService.page(clientInfo);
		
		logService.addLog(userId, "查询会员列表", 0);
		return new ObjectDataResponse<Page<ClientInfo>>(datas);
	}
	
	@RequestLogging
	@ApiOperation(value = "查询会员个人信息" , notes = "查询会员个人信息")
	@RequestMapping(value = "/queryClientInfoById", method = RequestMethod.POST)
	@ResponseBody
	public Response queryClientInfoById(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@ApiParam(value = "会员ID", name = "tableId", required = true)@RequestBody String tableId){
		
		ClientInfo datas = clientInfoService.getClientInfoById(tableId);
		
		logService.addLog(userId, "查询会员信息", 0);
		return new ObjectDataResponse<ClientInfo>(datas);
	}
	
	@RequestLogging
	@ApiOperation(value = "获取会员详细信息（包括认证、银行卡）" , notes = "获取会员详细信息（包括认证、银行卡）")
	@RequestMapping(value = "/getClientInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response getClientInfo(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
		
		ClientInfo datas = clientInfoService.getClientInfoById(userId.toString());
		
		logService.addLog(userId, "获取会员详细信息", 0);
		return new ObjectDataResponse<ClientInfo>(datas);
	}
	
	@RequestLogging
	@ApiOperation(value = "绑卡认证—保存银行卡信息" , notes = "绑卡认证—保存银行卡信息")
	@RequestMapping(value = "/handleClientBankCard", method = RequestMethod.POST)
	@ResponseBody
	public Response handleClientBankCard(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId, @RequestBody ClientBankCard clientBankCard){
		
		int success = clientInfoService.handleClientBankCard(userId, clientBankCard);
		
		logService.addLog(userId, "银行卡绑定", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "查询会员通讯录信息" , notes = "查询会员通讯录信息")
	@RequestMapping(value = "/queryClientContact", method = RequestMethod.POST)
	@ResponseBody
	public Response queryClientContact(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@ApiParam(value = "会员ID", name = "tableId", required = true)@RequestBody String tableId){
		
		List<ClientContact> datas = clientContactService.getMsgByClientId(tableId);
		
		logService.addLog(userId, "查询会员通讯录", 0);
		return new ObjectDataResponse<List<ClientContact>>(datas);
	}
	
	@IgnoreAuth
	@RequestLogging
	@ApiOperation(value = "查询会员认证列表信息" , notes = "查询会员认证列表信息")
	@RequestMapping(value = "/queryClientAuth", method = RequestMethod.POST)
	@ResponseBody
	public Response queryClientAuth(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody ClientInfo clientInfo){
		
		clientInfo.setOrderBy("ZX_CLIENT_INFO.create_time DESC");//降序
		Page<ClientInfo> datas=clientInfoService.findClientAuth(clientInfo);
		
		logService.addLog(userId, "查询会员认证列表", 0);
		return new ObjectDataResponse<Page<ClientInfo>>(datas);
	}
	
	@RequestLogging
	@ApiOperation(value = "修改会员详细信息" , notes = "修改会员详细信息")
	@RequestMapping(value = "/updateClientInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response updateClientInfo(@CurrentUserId Long userId,@RequestBody  ClientInfo clientInfo){
		clientInfo.setClientId(userId);
		int success = clientInfoService.handleClientUpdate(clientInfo);
		
		logService.addLog(userId, "修改会员详细信息", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@RequestLogging
	@ApiOperation(value = "添加会员详细信息" , notes = "添加会员详细信息")
	@RequestMapping(value = "/addClientInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response addClientInfo(@CurrentUserId Long userId,HttpServletRequest request,@RequestBody  ClientInfo clientInfo){
		return new ObjectDataResponse<Integer>(0);
	}
	
	/**
	 * @Description:移动端保存会员通讯信息
	 */
	@RequestLogging
	@ApiOperation(value = "获取会员联系人信息列表" , notes = "获取会员联系人信息列表")
	@RequestMapping(value = "/getClientContactMsg", method = RequestMethod.POST)
	@ResponseBody
	public Response getClientContactMsg(@CurrentUserId Long userId,@RequestBody  ClientContact clientContact){
		clientContact.setClientId(userId);
		int success = clientInfoService.handleClientContact(clientContact);
		return new ObjectDataResponse<Integer>(success);
	}
	/**
  	 * @Description:获取会员通讯信息
  	 * 通讯录、通话记录、紧急联系人、短信记录
  	 */
	@ApiOperation(value = "会员通讯信息（0108）", notes = "会员通讯信息（0108）")
	@RequestMapping(value = "/getClientContacts", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse <Map<String, Object>> getClientContacts(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@ApiParam(value = "会员ID", name = "tableId", required = true)@RequestBody String tableId){
		Map<String, Object> resultmMap = clientContactService.getClientContacts(tableId);
		
		return new ObjectDataResponse <Map<String, Object>>(resultmMap);
	}
	
	
}
